<template>
	<view class="center">
		<uni-sign-in ref="signIn"></uni-sign-in>
		<view class="userInfo" >
			<cloud-image width="150rpx" height="150rpx" v-if="hasLogin&&userInfo.avatar_file&&userInfo.avatar_file.url" :src="userInfo.avatar_file.url" @click.capture="toUserInfo"></cloud-image>
			<view v-else class="defaultAvatarUrl" @click.capture="toUserInfo">
				<image src="/static/logo.png" mode="widthFix" style="width: 60px;"></image>
			</view>
			
			<view class="logo-title">
				<text class="uer-name" @click.capture="toUserInfo" v-if="hasLogin">{{userInfo.nickname||userInfo.username||userInfo.mobile}}</text>
				<text class="uer-name" @click.capture="toUserInfo" v-else>{{$t('mine.notLogged')}}</text>
			</view>
			<view class="organize">
				<text v-if="hasLogin && roleType" :class="'user-role-'+roleType" @click.stop="toOrganize">{{roleTypes[roleType]}}</text>
				<text v-if="hasLogin && roleType"  @click.stop="toOrganize" class="toOrganize">上下级</text>
			</view>
		</view>
		<view class="grid-counts && hasLogin">
			<view class="grid-count-item" v-for="(item,index) in gridList" @click.native="tapGrid(index)" :key="index">
				<!-- <uni-icons class="icon" color="#007AFF" :type="item.icon" size="26"></uni-icons> -->
				<text class="grid-count">{{item.count}}</text>
				<text class="text">{{item.text}}</text>
				
			</view>
		</view>
		<view class="plan-grid" v-if="gridPlans.length && hasLogin">
			<view class="plan-main-title">
				月度任务
			</view>
			<view class="plan-info-flex" v-for="(item,index) in gridPlans">
				
				<view class="plan-info">
					<text class="plan-vlaue">
						{{item['count']}}
					</text>
					<text class="plan-desc">{{item['text']}}</text>
				</view>
			</view>
		</view>
		<uni-list class="center-list" v-for="(sublist , index) in ucenterList" :key="index">
			<uni-list-item v-for="(item,i) in sublist" :title="item.title" link :rightText="item.rightText" :key="i"
				:clickable="true" :to="item.to" @click="ucenterListClick(item)" :show-extra-icon="true"
				:extraIcon="{type:item.icon,color:'#999'}">
				<template v-slot:footer>
					<view v-if="item.showBadge" class="item-footer">
						<text class="item-footer-text">{{item.rightText}}</text>
						<view class="item-footer-badge"></view>
					</view>
				</template>
			</uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
	import callCheckVersion from '@/uni_modules/uni-upgrade-center-app/utils/call-check-version'
	const db = uniCloud.database();
	const Obj=uniCloud.importObject('mydb-customer')
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
	export default {
		data() {
			return {
				roleTypes:['','业务员','团队长','管理员','超级管理员'],
				roleType:null,
				gridList: [],
				ucenterList: [],
				gridPlans:[],
				listStyles: {
					"height": "150rpx", // 边框高度
					"width": "150rpx", // 边框宽度
					"border": { // 如果为 Boolean 值，可以控制边框显示与否
						"color": "#eee", // 边框颜色
						"width": "1px", // 边框宽度
						"style": "solid", // 边框样式
						"radius": "100%" // 边框圆角，支持百分比
					}
				}
			}
		},
		computed: {
			userInfo() {
				return store.userInfo
			},
			hasLogin(){
				return store.hasLogin
			},
			appConfig() {
				return getApp().globalData.config
			}
		},
		onShow() {
			db.collection('mydb-permission').where({user_id:this.userInfo._id}).get().then((res)=>{
				console.log(res)
				this.roleType=res.result.data.length ? res.result.data[0]['type'] : 1
			})
			Obj.getCenterCount().then((res)=>{
				console.log(res)
				this.gridList=res.result.gridCounts || []
				this.ucenterList=res.result.userMenus || []
				this.gridPlans=res.result.gridPlans || []
			})
		},
		methods: {
			toSettings() {
				uni.navigateTo({
					url: "/pages/ucenter/settings/settings"
				})
			},
			signIn() { //普通签到
				this.$refs.signIn.open()
			},
			signInByAd(){ //看激励视频广告签到
				this.$refs.signIn.showRewardedVideoAd()
			},
			/**
			 * 个人中心项目列表点击事件
			 */
			ucenterListClick(item) {
				if (!item.to && item.event) {
					this[item.event]();
				}
			},
			async checkVersion() {
				let res = await callCheckVersion()
				console.log(res);
				if (res.result.code > 0) {
					checkUpdate()
				} else {
					uni.showToast({
						title: res.result.message,
						icon: 'none'
					});
				}
			},
			toUserInfo() {
				uni.navigateTo({
					url: '/uni_modules/uni-id-pages/pages/userinfo/userinfo'
				})
			},
			toOrganize() {
				uni.navigateTo({
					url: '/pages/ucenter/organize/organize'
				})
			},
			tapGrid(index) {
				
			},
			/**
			 * 获取积分信息
			 */
			getScore() {
				if (!this.userInfo) return uni.showToast({
					title: this.$t('mine.checkScore'),
					icon: 'none'
				});
				uni.showLoading({
					mask: true
				})
				db.collection("uni-id-scores")
					.where('"user_id" == $env.uid')
					.field('score,balance')
					.orderBy("create_date", "desc")
					.limit(1)
					.get()
					.then((res) => {
						console.log(res);
						const data = res.result.data[0];
						let msg = '';
						msg = data ? (this.$t('mine.currentScore')+ data.balance) : this.$t('mine.noScore');
						uni.showToast({
							title: msg,
							icon: 'none'
						});
					}).finally(()=>{
						uni.hideLoading()
					})
			},
			async share() {
				let {result} = await db.collection('uni-id-users').where("'_id' == $cloudEnv_uid").field('my_invite_code').get()
				let myInviteCode = result.data[0].my_invite_code
				if(!myInviteCode){
					return uni.showToast({
						title: '请检查uni-config-center中uni-id配置，是否已启用 autoSetInviteCode',
						icon: 'none'
					});
				}
				console.log({myInviteCode});
				let {
					appName,
					logo,
					company,
					slogan
				} = this.appConfig.about
			
			}
		}
	}
</script>

<style lang="scss" scoped>
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}

	page,.page {
		background-color: #f8f8f8;
	}
	
	.center {
		flex: 1;
		flex-direction: column;
		background-color: #f8f8f8;
	}

	.userInfo {
		padding-top: 80px;
		flex-direction: column;
		align-items: center;
		// background-image: url('/static/uni-center/head.jpg');
		background-image: linear-gradient(360deg, #f8f8f8, #fff);
		background-size: cover;
	}
	.defaultAvatarUrl{
		border-radius: 100%;
		justify-content: center;
		align-items: center;
	}

	.logo-title {
		flex: 1;
		align-items: center;
		justify-content: space-between;
		flex-direction: row;
	}

	.uer-name {
		height: 32px;
		line-height: 32px;
		font-size: 38rpx;
		color: #666;
	}
	
	.user-role-4{
		color: #fff;
		font-size: 12px;
		border-radius: 4px;
		background: #ff3a54;
		padding: 2px 6px;
		margin-left: 4px;
	}
	.grid-counts {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		background-color: #FFFFFF;
		margin: 0 15px 10px;
		padding: 10px 20px;
		border-radius: 8px;
	}
	.grid-count-item{
		text-align: center;
	}
	.grid-count{
		font-size: 24px;
		font-weight: bold;
		color: #007AFF;
	}
	.uni-grid .text {
		font-size: 14px;
		height: 20px;
		line-height: 20px;
		color: #999;
	}

	.uni-grid .item ::v-deep .uni-grid-item__box {
		justify-content: center;
		align-items: center;
	}

	.center-list{
		margin: 0 15px 10px;
		border-radius: 8px;
	}
	/*修改边线粗细示例*/
	/* #ifndef APP-NVUE */
	.center-list ::v-deep .uni-list--border:after {
		-webkit-transform: scaleY(0.2);
		transform: scaleY(0.2);
		margin-left: 80rpx;
	}

	.center-list ::v-deep .uni-list--border-top,
	.center-list ::v-deep .uni-list--border-bottom {
		display: none;
	}

	/* #endif */
	.item-footer {
		flex-direction: row;
		align-items: center;
	}

	.item-footer-text {
		color: #999;
		font-size: 24rpx;
		padding-right: 10rpx;
	}

	.item-footer-badge {
		width: 20rpx;
		height: 20rpx;
		/* #ifndef APP-NVUE */
		border-radius: 50%;
		/* #endif */
		/* #ifdef APP-NVUE */
		border-radius: 10rpx;
		/* #endif */
		background-color: #DD524D;
	}
	.plan-grid{
		display: flex;
		justify-content: space-between;
		flex-direction: row;
		flex-wrap: nowrap;
		margin: 0 15px 10px;
		padding: 15px;
		padding-top: 35px;
		border-radius: 8px;
		background-color: #fff;
		position: relative;
	}
	.plan-main-title{
		position: absolute;
		left: 15px;
		top: 12px;
		font-size: 14px;
		color: #333;
	}
	.plan-info-flex{
		flex:1;
		color: #666;
		font-size: 14px;
		display: flex;
		align-items: center;
		justify-content: center;
		
	}
	.plan-title{
		text-align: left;
	}
	.plan-vlaue{
		font-size: 20px;
		font-weight: bold;
		color: #000;
		text-align: center;
	}
	.plan-desc{
		color: #999;
	}
	.user-role-1{
		color: #fff;
		font-size: 12px;
		background: #666;
		padding: 2px 6px;
		border-radius: 4px;
		margin-left: 4px;
	}
	.user-role-2{
		color: #fff;
		font-size: 12px;
		border-radius: 4px;
		background: #007AFF;
		padding: 2px 6px;
		margin-left: 4px;
	}
	.user-role-3{
		color: #fff;
		font-size: 12px;
		border-radius: 4px;
		background: #ffad20;
		padding: 2px 6px;
		margin-left: 4px;
	}
	.user-role-4{
		color: #fff;
		font-size: 12px;
		border-radius: 4px;
		background: #a3c5e3;
		padding: 2px 6px;
		margin-left: 4px;
	}
	.user-role-5{
		color: #fff;
		font-size: 12px;
		border-radius: 4px;
		background: #ff557f;
		padding: 2px 6px;
		margin-left: 4px;
	}
	.organize{
		display: flex;
		align-items: center;
		flex-direction: row;
		margin: 10px 0;
	}
	.toOrganize{
		color: #7b847e;
		font-size: 12px;
		text-decoration: underline;
		margin-left: 4px;
	}
</style>
